﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public class KA41HLTReport : BaseReport
    {
        string strCompany = "HLT";
        string strExcelFile = "";
        string sheetName = "";
        string Standard = "GBMOV";
        string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\HLT\HLTPlanningReport.xls");
        public KA41HLTReport()
            : base()
        {
        }
        public KA41HLTReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            strExcelFile = @"FLT\FLTInventory.xls";
            /*
             *GMMANDANT, GMJJJJ, GMMM, GMTT, GMLO,  GMLB, GMGART, GMGVAR, 
GMGCOLOR, GMGDES, GMPARTIE, GMBATCH,  GMSTARTQTY, GMEKIN, GMELSEIN,  GMABOUT, GSYJOUT, 
GSEXPROUT, GMEXDYEOUT,  GMDEVOUT,  GMEKOUT,  GMENDQTY,  GMEXPOUT, GMEXPIN,  GMABYARN,  GMYJYARN, 
 GMDYEDYARN,  GMENDDAT,  GMSTARTDAT
             */
            #region Excel  columns
            columns.Add("GMLO"); 
            columns.Add("GMGDES");
            columns.Add("GMGART");
            columns.Add("GMGVAR");
            columns.Add("GMGCOLOR");
            columns.Add("GMPARTIE");
            columns.Add("GMSTARTQTY");
            columns.Add("GMEXPOUT");
            columns.Add("GMEXPIN");
            columns.Add("GMELSEIN");
            columns.Add("GMENDQTY");
            //GMGART	GMGVAR	GMGCOLOR	GMPARTIE	GMSTARTQTY	GSEXPROUT	GMEXPIN	GMELSEIN	GMENDQTY

            #endregion 
        }
        public void FillInExcelData(ArrayList columns, DataSet setSoruce, int xStart = 7, int yStart = 1)
        {
            try
            {
                FillinHead();
                FillInStage(9, 3);
                FillInYarn("J",12);
                FillInYarn("W", 23);
                FillInProcess(36,4);
                FillInProcess(39, 5);
                FillInProcess(40, 6);
                FillInProcess(49, 9,true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FillInStage(int stage, int startrow)
        { 
            try
            {
                DataTable tbMaster = dsSource.Tables["QUTMASTER"];
                string cond = "";
                int row = startrow;
                DataRow[] rows = null;
                cond = "QUSTAGE='"+stage+"'";
                rows = tbMaster.Select(cond);
                DataRow rowx = null;
                foreach (DataRow dwTemp in rows)
                {
                    rowx = dwTemp;
                    exAgent.SetCellValue(row, 29, dwTemp["QUCPRICE"].ToString());
                }
                if (stage == 9)
                {
                    exAgent.SetCellValue(57, 7, rowx["QURATIO"].ToString());
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FillInYarn(string jw, int startrow)
        {
            try
            {
                DataTable tbMaster = dsSource.Tables["QUTMASTER"];
                DataTable tbGSDAT = dsSource.Tables["GSDAT"];
                DataTable tbGASTGA = dsSource.Tables["GASTGA"];
                string cond = "";
                DataRow[] rows = null;
                DataRow[] rowb = null;
                cond = "QUSTAGE='22' and QUJW='J'";
                if (jw == "W")
                {
                    cond = "QUSTAGE='21' and QUJW='W'";
                } 
                rows = tbMaster.Select(cond);
                int row = startrow;
                string art = "";
                string var = "";
                string color = "";
                string sbomid = "";
                string ends = "";
                foreach (DataRow dwTemp in rows)
                {
                     sbomid = dwTemp["QUBOMID"].ToString();
                     cond = "QUSBOMID='" + sbomid + "'";
                     rowb = tbMaster.Select(cond);
                     ends = dwTemp["QUQTY1"].ToString();                     
                     foreach (DataRow dwBom in rowb)
                     {
                         art = dwBom["QUARTICLE"].ToString();
                         var = dwBom["QUVAR"].ToString();
                         color = dwBom["QUCOLOR"].ToString();
                         exAgent.SetCellValue(row, 2, art);
                         exAgent.SetCellValue(row, 8, color);
                         exAgent.SetCellValue(row, 14, ends);
                         exAgent.SetCellValue(row, 16, System.Convert.ToDouble(dwBom["QUQUANTITY"].ToString()) * 1000);
                         exAgent.SetCellValue(row, 19, dwBom["QUCPRICE"].ToString());
                         exAgent.SetCellValue(row, 21, dwBom["QUFPRICE"].ToString());
                         exAgent.SetCellValue(row, 23, dwBom["QUCVALUE"].ToString());
                         exAgent.SetCellValue(row, 26, dwBom["QUSUPNAME"].ToString());
                         row++;
                     }
                     if (dwTemp["QUSOURCETYPE"].ToString() == "P")
                     {
                         art = dwTemp["QUARTICLE"].ToString();
                         var = dwTemp["QUVAR"].ToString();
                         color = dwTemp["QUCOLOR"].ToString();
                         exAgent.SetCellValue(row, 2, art);
                         exAgent.SetCellValue(row, 8, color);
                         exAgent.SetCellValue(row, 16, System.Convert.ToDouble(dwTemp["QUQUANTITY"].ToString()) * 1000);
                         exAgent.SetCellValue(row, 19, dwTemp["QUCPRICE"].ToString());
                         exAgent.SetCellValue(row, 21, dwTemp["QUFPRICE"].ToString());
                         exAgent.SetCellValue(row, 23, dwTemp["QUCVALUE"].ToString());
                         exAgent.SetCellValue(row, 26, dwTemp["QUSUPNAME"].ToString());
                         row++;                  
                     }
                    //用纱量g			现在		将来		(元/米)			供应商			报价日期		
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FillInProcess(int startrow,int stage,bool isadmin=false)
        {
            try
            {
                DataTable tbProcess= dsSource.Tables["QUPROCESS"];
                DataTable tbGSDAT = dsSource.Tables["GSDAT"];
                DataTable tbGASTGA = dsSource.Tables["GASTGA"];
                string cond = "";
                DataRow[] rows = null;
                DataRow[] rowb = null;
                cond = "QPSTAGE= '"+stage.ToString()+"'";
                rows = tbProcess.Select(cond);
                int row = startrow;
                string art = "";
                string var = "";
                string color = "";
                string sbomid = "";

                foreach (DataRow dwTemp in rows)
                {
                    if (!isadmin)
                    {
                        exAgent.SetCellValue(row, 2, dwTemp["QPPROCDES"].ToString());
                        exAgent.SetCellValue(row, 11, dwTemp["QPCPRICE"].ToString());
                        exAgent.SetCellValue(row, 16, dwTemp["QPCVALUE"].ToString());
                        exAgent.SetCellValue(row, 21, dwTemp["QPSUPPLIER"].ToString());
                    }
                    else
                    {
                        exAgent.SetCellValue(row, 2, dwTemp["QPPROCDES"].ToString());
                        exAgent.SetCellValue(row, 7, dwTemp["QPCPRICE"].ToString());
                    }
                   row++;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FillinHead()
        {
            try
            {
                string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                exAgent.SetCellValue(2, 4, filedate);
                DataRow dwKAAUF = dsSource.Tables["KAAUF"].Rows[0];
                DataRow dwWASTAMM = dsSource.Tables["WASTAMM"].Rows[0];
                DataRow dwBSSATZ = dsSource.Tables["BSSATZ"].Rows[0];
                 
                string Article = dwKAAUF["KAAART"].ToString() + "-" + dwKAAUF["KAAVAR"].ToString()
              + "-" + dwKAAUF["KAAFARBE"].ToString() + "-" + dwKAAUF["KAABESCHI"].ToString();
                exAgent.SetCellValue(6, 4, dwKAAUF["KAAART"].ToString());
                exAgent.SetCellValue(6, 11, dwKAAUF["KAAFARBE"].ToString());
                exAgent.SetCellValue(6, 16, dwKAAUF["KAABESCHI"].ToString());
                exAgent.SetCellValue(6, 22, dwKAAUF["KAAFARBE"].ToString());
                //总头份：						内经头份		

                	//纬密(根/英寸)																	克重(g/M)										坯布幅宽cm						边经头份		
                string wpd=System.Math.Round(System.Convert.ToDouble(dwWASTAMM["WAFFDDIK"].ToString())*0.01,0).ToString();
                string wfd = System.Math.Round(System.Convert.ToDouble(dwWASTAMM["WAFFDDIS"].ToString()) * 0.01, 0).ToString();
                string um=dwWASTAMM["WAFDDIM"].ToString();
                //J/W密
                exAgent.SetCellValue(7, 4, "(根/" + um + ")");
                exAgent.SetCellValue(7, 8, wpd + "x" + wfd);
                // 克重
                exAgent.SetCellValue(7, 16, dwWASTAMM["WARGRLFD"].ToString());
                // 坯布幅宽
                exAgent.SetCellValue(7, 26, System.Math.Round(System.Convert.ToDouble(dwWASTAMM["WARWBRT"].ToString()) * 0.1, 0).ToString());
                exAgent.SetCellValue(6, 26, dwBSSATZ["BSFDZ"].ToString());
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void WriteExcelReport(string fileType)
        {
            try
            {
                string filename = "";
                string sheetName = "";
                string Standard = "QUOTATION";
                strExcelFile = @"HLT\HLTQuotation.xls";
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\" + strExcelFile);
                if (!dsSource.Tables.Contains("KAAUF"))
                {
                    return;
                }
                if (dsSource.Tables["KAAUF"].Rows.Count == 0)
                    return;
                DataRow dwKAAUF = dsSource.Tables["KAAUF"].Rows[0];
                string Article = dwKAAUF["KAAART"].ToString().Trim() + "_" + dwKAAUF["KAAVAR"].ToString().Trim()
              + "_" + dwKAAUF["KAAFARBE"].ToString().Trim() + "_" + dwKAAUF["KAABESCHI"].ToString().Trim();
                exAgent.Open(filePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                filename = @"报价\" + Article + "_" + filedate + ".xls";
                string file = Path.Combine(PathHelper.ApplicationPath, @"Report\" + filename);
                sheetName = DateTime.Now.ToShortTimeString().Replace(@":", "");
                if (System.IO.File.Exists(file))
                {
                    System.IO.File.Delete(file);
                }
                exAgent.CreateNewSheet(filename, Standard, sheetName);
                FillInExcelData(columns, dsSource, 7, 1);
                base.WriteExcelReport(fileType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
